/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.pneu.pneuservisbackend.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import pa165.pneu.pneuservisbackend.entiies.Customer;

/**
 *
 * @author Juraj Palenik
 */
@Repository
public class CustomerDAOImpl implements CustomerDAO{
    
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void createCustomer(Customer customer) {
        if(customer == null || customer.getId() != null || customer.getAddress() == null || 
                customer.getName() == null || customer.getTelephone() == null) {
            throw new IllegalArgumentException("Create customer called with wrong param.");
        } 

        entityManager.persist(customer);

    }

    @Override
    public Customer getCustomer(Long id) {
        if(id == null) {
            throw new IllegalArgumentException("getCustomer called with null param.");
        }
        
        Customer result = entityManager.find(Customer.class,id);
        entityManager.detach(result);
        return result;
    }

    @Override
    public void updateCustomer(Customer customer) {
        if(customer == null || customer.getId() == null || customer.getAddress() == null || 
                customer.getName() == null || customer.getTelephone() == null) {
            throw new IllegalArgumentException("Update customer called with wrong param");
        }
        
        Customer find = entityManager.find(Customer.class, customer.getId());
        if(find == null) {
            throw new IllegalArgumentException(customer +" is not in db, but attempt to update.");
        }
        if(!customer.equals(find)){
            throw new IllegalArgumentException("Very fucked up.");
        }
        
        entityManager.merge(customer);
        
    }

    @Override
    public void removeCustomer(Customer customer) {
        if(customer == null || customer.getId() == null) {
            throw new IllegalArgumentException("Remove customer called with wrong param");
        }
        Customer result = entityManager.find(Customer.class,customer.getId());
        entityManager.remove(result);
    }

    @Override
    public List<Customer> getAllCustomers() {
        List<Customer> result;
        
        Query query = entityManager.createQuery("SELECT c FROM Customer c ORDER BY c.id");
        result = query.getResultList();
        entityManager.detach(result);
        return result;
    }

    @Override
    public List<Customer> findCustomerByName(String name) {
        
        List<Customer> result;
        Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name ORDER BY c.id");
        query.setParameter("name", name);
        result = query.getResultList();
        entityManager.detach(result);
        return result;
    }
    
}
